package com.atguigu.common.config;

import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author Administrator
 * @version 1.0
 * @description:  自定义异常处理器
 * @date 2021/7/13 0013 14:02
 */
@Slf4j
@Component
@RestControllerAdvice //是对Controller进行增强的，可以全局捕获spring mvc抛的异常。
public class UnifiedExceptionHandler {


    @ExceptionHandler(value = Exception.class) //
    public Result handleException(Exception e){
        log.error(e.getMessage(),e);
        return Result.error().message("服务器出现异常，请稍后");
    }
    @ExceptionHandler(value = ArithmeticException.class) //
    public  Result   isArithmeticException(ArithmeticException exception){
        log.error(exception.getMessage(),exception);
        return Result.setResult(ResponseEnum.ERROR);
    }

    /** 
     * @description: 自定义异常处理 
     * @param: businessException 
     * @return: com.atguigu.common.result.Result 
     * @author Administrator
     * @date: 2021/7/13 0013 15:11
     */ 
    @ExceptionHandler(value = BusinessException.class) //
    public Result isBusinessException(BusinessException businessException){
        log.error(businessException.getMessage(),businessException);
        return Result.error().code(businessException.getCode()).message(businessException.getMessage());
    }



    /**
     * Controller上一层相关异常，在controller还没有处理就已经是异常了，前台传值问题等
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public Result handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return Result.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }

}
